package cn.edu.bupt.freewings.forum.desktop.view;

import java.util.List;
import java.util.LinkedList;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JDialog;

import cn.edu.bupt.freewings.forum.common.PostType;
import cn.edu.bupt.freewings.forum.client.rest.PostClient;
import cn.edu.bupt.freewings.forum.desktop.utility.CommonUtility;
/**
 * @author hubaiyu (hubaiyu@gmail.com)
 * 
 */
public class MainWindow extends JFrame {
	List<JLabel> messageLables = new LinkedList<JLabel>();
	JPanel messagePanel = new JPanel();
	JPanel buttonsPanel = new JPanel();
	JPanel controlPanel = new JPanel();  //Bottom Panel
	JTextArea postMessage = new JTextArea();
	JButton refreshButton = new JButton("Refresh");
	JButton postButton = new JButton("Post");
	JButton serverButton = new JButton("Set Server");
	ServerConfigDialog configDialog = new ServerConfigDialog(this, "Server Config", this);

  static{
  	setServer("http://118.229.145.190:8080/server");
  }
	
  private static void setServer(String server){
  	cn.edu.bupt.freewings.forum.client.Constants.setRestServer(server);
  }
  
	public MainWindow() {
		this.setTitle("Freewings Forum Desktop Client");
		this.initComponentsStyle();
		this.pasteComponents();
		this.addEvents();
		this.setVisible(true);
	}

	private void initComponentsStyle() {
		this.setSize(400, 600);
		this.setLayout(new BorderLayout());

		messagePanel.setLayout(new GridLayout(15, 1));

		buttonsPanel.setLayout(new GridLayout(1, 3));
		
		controlPanel.setLayout(new BorderLayout());
	}

	private void pasteComponents() {
		buttonsPanel.add(refreshButton);
		buttonsPanel.add(postButton);
		buttonsPanel.add(serverButton);
		
		controlPanel.add(postMessage, BorderLayout.CENTER);
		controlPanel.add(buttonsPanel, BorderLayout.SOUTH);
		controlPanel.setSize(400, 60);
		
		this.getContentPane().add(messagePanel, BorderLayout.CENTER);
		this.getContentPane().add(controlPanel, BorderLayout.SOUTH);	
	}

	private void addEvents(){
		final MainWindow target = this;
		refreshButton.addMouseListener(new MouseAdapter(){
			@Override
			public void mouseReleased(MouseEvent e) {
				target.refresh();
			}
		});	
		
		postButton.addMouseListener(new MouseAdapter(){
			@Override
			public void mouseReleased(MouseEvent e){
				if(postMessage.getText() == null || "".equals(postMessage.getText().trim()))
						return;
				new PostClient().put(CommonUtility.makePost(postMessage.getText(), 3, 1));
				postMessage.setText("");
				target.refresh();
			}
		});
		
		serverButton.addMouseListener(new MouseAdapter(){
			@Override
			public void mouseReleased(MouseEvent e) {
				configDialog.setInitText(cn.edu.bupt.freewings.forum.client.Constants.REST_SERVER);
				configDialog.setVisible(true);
				if(!"".equals(configDialog.getText().trim()))
					setServer(configDialog.getText());
			}
		});	
		
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public void refresh() {
		PostType.PostList posts = new PostClient().getByTopicId(3, 0, 100);
		System.out.println("Get posts");
		if (messageLables.size() < posts.getPostList().size()) {
			List<JLabel> newLables = new LinkedList<JLabel>();
			for (PostType.Post post : posts.getPostList()) {
				System.out.println(post.getText());
				JLabel label = new JLabel();
				label.setText(post.getUser().getNickName() + " : " + post.getText());
				newLables.add(label);
			}
			messageLables = newLables;
		}
		postLablesToFrame(messageLables);
	}

	private void postLablesToFrame(List<JLabel> labels) {
		messagePanel.removeAll();
		for (JLabel label : labels) {
			messagePanel.add(label);
		}
		this.setVisible(true);
	}

	public static void main(String args[]) {
		new MainWindow().refresh();
	}
}
